package com.uenpay.im.ui.old.activity.movement;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.weight.edittext.ExpressionUtil;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.ui.old.activity.dynamic.Praise;
import com.uenpay.im.ui.old.activity.dynamic.Reply;
import com.uenpay.im.panel.emoji.EmojiAdapter;
import com.uenpay.im.weight.MyDialog;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-1-21 下午8:37:57
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class ReplyHandler {

	public Context mContext;

	public ReplyHandler(Context _Context) {
		mContext = _Context;
	}

	public void setPraiseView(ArrayList<Praise> praise, LinearLayout praise_ll,
			TextView praise_tv) {
		if (praise != null && praise.size() > 0) {
			praise_ll.setVisibility(View.VISIBLE);
			String s = "";
			for (int i = 0; i < praise.size(); i++) {
				if (i == praise.size() - 1) {
					s += praise.get(i).nickname.trim();
				} else {
					s += praise.get(i).nickname.trim() + ",";
				}
			}
			s = Utils.ToDBC(s);
			praise_tv.setText(s);
		} else {
			praise_ll.setVisibility(View.GONE);
		}
	}

	public View getReplyTextView(EditText _editText, Reply r,
			LinearLayout faLin, ITextLinkListener mLinkListener) {
		View view = LayoutInflater.from(mContext).inflate(R.layout.item_reply,
				null);
		TextView reply_all_tv = (TextView) view.findViewById(R.id.reply_all_tv);
		SpannableStringBuilder ssb = new SpannableStringBuilder();
		String reply = mContext.getResources()
				.getString(R.string.dynamic_reply);
		if (r.pcustid == 0) {
			ssb.append(r.nickname.trim() + ": ");
		} else {
			ssb.append(r.nickname.trim() + reply + r.pnickname.trim() + ": ");
		}
		TextLinkListener tllf = new TextLinkListener(_editText, 0, r, faLin, 0,
				mLinkListener);

		TextLinkListener tllr = new TextLinkListener(_editText, 0, r, faLin,
				r.pcustid, mLinkListener);
		ssb.setSpan(tllf, 0, r.nickname.length(),
				Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		ssb.append(ExpressionUtil.getExpressionString(mContext, r.content
				.trim().replace(" ", "")));

		TextLinkListener tlll = new TextLinkListener(_editText, 2, r, faLin, 0,
				mLinkListener);

		if (r.pcustid != 0) {

			ssb.setSpan(new ForegroundColorSpan(Color.BLACK),
					r.nickname.length(), r.nickname.length() + reply.length(),
					Spanned.SPAN_INCLUSIVE_INCLUSIVE);
			ssb.setSpan(
					tllr,
					r.nickname.length() + reply.length(),
					r.nickname.length() + r.pnickname.length() + reply.length(),
					Spanned.SPAN_INCLUSIVE_INCLUSIVE);
			ssb.setSpan(tlll, r.nickname.length() + reply.length()
					+ r.pnickname.length() + 1, ssb.length(),
					Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		} else {

			ssb.setSpan(tlll, r.nickname.length() + 1, ssb.length(),
					Spanned.SPAN_INCLUSIVE_INCLUSIVE);
		}

		reply_all_tv.setMovementMethod(LinkMovementMethod.getInstance());
		reply_all_tv.setText(ssb);
		return view;

	}

	public interface ITextLinkListener {
		abstract void onTextLink(int status, String custId, String pCustId);
	};

	public class TextLinkListener extends ClickableSpan {
		private int type;
		private Reply r;
		private int source_id;
		private EditText editText;
		private ITextLinkListener mLinkListener;

		public TextLinkListener(EditText _editText, int type, Reply reply,
				LinearLayout linearLayout, int source_cust_id,
				ITextLinkListener _LinkListener) {
			this.type = type;
			this.r = reply;
			this.source_id = source_cust_id;
			this.mLinkListener = _LinkListener;
			this.editText = _editText;
		}

		@Override
		public void onClick(View widget) {
			switch (type) {
			case 0:
				if (source_id == 0) {
					ActivityHandle.startUserDetailActivity((Activity) mContext,
							r.custid);
				} else {
					ActivityHandle.startUserDetailActivity((Activity) mContext,
							r.pcustid + "");
				}

				break;
			case 1:
				break;
			case 2:
				final String runame = r.nickname;
				final String rid = r.replyid;
				if (r.custid.equals(IMApplication.getCustId())) {

					MyDialog.Builder alertDialog = new MyDialog.Builder(
							mContext);
					alertDialog.setTitle(R.string.dialog_title);
					alertDialog.setMessage(mContext.getResources().getString(
							R.string.delete_dynamic_dialog));
					alertDialog.setPositiveButton(R.string.sure,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
									mLinkListener.onTextLink(0, rid, source_id
											+ "");
								}
							});

					alertDialog
							.setNegativeButton(
									R.string.cancel,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
										}
									});
					alertDialog.create().show();

				} else {
					editText.setHint(mContext.getResources()
							.getString(R.string.dynamic_reply).trim()
							+ runame.trim() + ":");
					mLinkListener.onTextLink(1, rid, r.custid + "");
				}

				break;
			default:
				break;
			}
		}

		@Override
		public void updateDrawState(TextPaint ds) {
			if (type == 0) {
				ds.setColor(mContext.getResources().getColor(
						R.color.reply_name_color));
			} else {
				ds.setColor(mContext.getResources().getColor(
						R.color.the_color_black));
			}
			ds.setTextSize(mContext.getResources().getDimension(
					R.dimen.font_size_14sp));
			ds.setUnderlineText(false);
		}
	}

	public void DeleteEmoji(EditText editText, String emoji_name) {
		if (editText != null) {
			int start = editText.getSelectionStart();
			String content = editText.getText().toString();
			if (TextUtils.isEmpty(content) || start == 0) {
				return;
			}
			String startContent = content.substring(0, start);
			String endContent = content.substring(start, content.length());
			String lastContent = content.substring(start - 1, start);
			int last = startContent.lastIndexOf("[" + emoji_name);
			int lastChar = startContent.substring(0, startContent.length() - 1)
					.lastIndexOf("] ");

			if ("]".equals(lastContent) && last > lastChar) {
				if (last != -1) {
					editText.setText(startContent.substring(0, last)
							+ endContent);
					CharSequence info = editText.getText();
					if (info instanceof Spannable) {
						Spannable spanText = (Spannable) info;
						Selection.setSelection(spanText, last);
					}
					return;
				}
			}
			editText.setText(startContent.substring(0, start - 1) + endContent);
			CharSequence info = editText.getText();
			if (info instanceof Spannable) {
				Spannable spanText = (Spannable) info;
				Selection.setSelection(spanText, start - 1);
			}
		}

	}

	public void showIndexMark(EmojiAdapter emojiAdapter,
			LinearLayout indexGroup, int index) {
		int max = 0;
		max = emojiAdapter.getCount();
		indexGroup.removeAllViews();
		for (int i = 0; i < max; i++) {
			ImageView image = new ImageView(mContext);
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.WRAP_CONTENT,
					LinearLayout.LayoutParams.WRAP_CONTENT);
			lp.leftMargin = 10;
			lp.rightMargin = 10;
			lp.topMargin = 5;
			lp.bottomMargin = 5;
			if (index == i) {
				image.setImageResource(R.drawable.point);
			} else {
				image.setImageResource(R.drawable.point_selected);
			}
			image.setLayoutParams(lp);
			indexGroup.addView(image);
		}
	}
}
